// FrogImageResizeDialog.cpp : Implementation of CFrogImageResizeDialog

#include "stdafx.h"
#include "FrogImageResizeDialog.h"
#include "FrogMessageBox.h"


// CFrogImageResizeDialog
CFrogImageResizeDialog::CFrogImageResizeDialog(HICON hIcon):
CFrogProgressReportDialog(hIcon),
m_ShrinkBy(50),
m_Width(640),
m_Height(480),
m_Quality(8),
m_ResizePercent(0),
m_SaveAsJpg(false),
m_ChangeSaveQuality(false),
m_PutInFolder(true),
m_Folder(L"Resized Images")
{
}

LRESULT CFrogImageResizeDialog::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	CFrogProgressReportDialog::OnInitDialog(uMsg, wParam, lParam, bHandled);

	m_EditWidth.Attach(GetDlgItem(IDC_EDIT_WIDTH));
	m_EditHeight.Attach(GetDlgItem(IDC_EDIT_HEIGHT));
	
	m_EditFolder.Attach(GetDlgItem(IDC_EDIT_FOLDER));

	m_LabelWidth.Attach(GetDlgItem(IDC_STATIC_W));
	m_LabelHeight.Attach(GetDlgItem(IDC_STATIC_H));

	m_SliderQuality.Attach(GetDlgItem(IDC_SLIDER_QUALITY));
	m_SliderPercent.Attach(GetDlgItem(IDC_SLIDER_PERCENT));	

	m_RadioPercent.Attach(GetDlgItem(IDC_RADIO_PERCENT));
	m_RadioSize.Attach(GetDlgItem(IDC_RADIO_CUSTOM_WH));

	m_CheckBoxQuality.Attach(GetDlgItem(IDC_CHECK_QUALITY));
	m_CheckBoxSaveAsJpeg.Attach(GetDlgItem(IDC_CHECK_JPG));
	m_CheckBoxPutInFolder.Attach(GetDlgItem(IDC_CHECK_PUT_IN_FOLDER));

	m_SliderPercent.SetRange(1,100);
	m_SliderQuality.SetRange(1,10);

	m_SliderPercent.EnableWindow(TRUE);

	m_EditWidth.EnableWindow(FALSE);
	m_EditHeight.EnableWindow(FALSE);
	m_LabelWidth.EnableWindow(FALSE);
	m_LabelHeight.EnableWindow(FALSE);
	
	if (!m_PutInFolder)
	{
		m_EditFolder.EnableWindow(FALSE);
	}

	m_SliderQuality.EnableWindow(FALSE);

	HWND spinHwndW = GetDlgItem(IDC_SPIN_W);
	SendMessage(spinHwndW, (UINT) UDM_SETRANGE, (WPARAM)0, (LPARAM)MAKELONG((short)UD_MAXVAL, (short)1));

	HWND spinHwndH = GetDlgItem(IDC_SPIN_H);
	SendMessage(spinHwndH, (UINT) UDM_SETRANGE, (WPARAM)0, (LPARAM)MAKELONG((short)UD_MAXVAL, (short)1));

	m_SliderPercent.SetPos(m_ShrinkBy);
	m_SliderQuality.SetPos(m_Quality);		

	DoDataExchange(FALSE);

	return 0;
}

LRESULT CFrogImageResizeDialog::OnClickedOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	bHandled = TRUE;
	// TODO: Add your control notification handler code here
	DoDataExchange(TRUE);
	m_ShrinkBy = m_SliderPercent.GetPos();
	m_Quality = m_SliderQuality.GetPos();		

	if (m_CheckBoxQuality.GetCheck() == FALSE)
	{
		m_Quality = -1;
	}

	if (m_CheckBoxPutInFolder.GetCheck() == TRUE)
	{
		if (!FilerFrogUtils::IsFileNameValid(wstring(m_Folder)))
		{
			FrogMessageBox(m_hWnd, L"Please Enter a Valid Folder Name", L"FilerFrog - Image Resize", 0);
			return TRUE;
		}
	}
	else
	{
		m_Folder = L"";
	}

	if (m_RadioPercent.GetCheck() == FALSE)
	{
		m_ShrinkBy = -1;

		if (m_Width <= 0 || m_Height <= 0)
		{
			FrogMessageBox(m_hWnd, L"Please Enter a Valid Size", L"FilerFrog - Image Resize", 0);
			return TRUE;
		}
	}
	else
	{
		if (m_ShrinkBy <= 0)
		{
			FrogMessageBox(m_hWnd, L"Please Enter a Valid Size", L"FilerFrog - Image Resize", 0);
			return TRUE;
		}
	}

	RunAsyncCommandWithProgressBar(false);

	EndDialog(wID);
	return 0;
}

void CFrogImageResizeDialog::ExecuteCommand()
{
	if (m_ShrinkBy > 0)
	{
		m_FilesRefactor.ShrinkImageFilesByPercent(m_LastResult, m_FilesList, m_ShrinkBy / 100.0, m_Quality, m_SaveAsJpg, wstring(m_Folder));
	}
	else
	{
		if (m_Width > 0 && m_Height > 0)
		{
			m_FilesRefactor.ShrinkImageFilesBySize(m_LastResult, m_FilesList, m_Width, m_Height, m_Quality, m_SaveAsJpg, wstring(m_Folder));
		}
		else
		{
			m_FilesRefactor.ShrinkImageFilesBySize(m_LastResult, m_FilesList, 640, 480, m_Quality, m_SaveAsJpg, wstring(m_Folder));
		}
	}
}

LRESULT CFrogImageResizeDialog::OnRadioPercentBnClicked(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	EnableDisableControls();
	return 0;
}

LRESULT CFrogImageResizeDialog::OnRadioCustomWhBnClicked(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	EnableDisableControls();
	return 0;
}

LRESULT CFrogImageResizeDialog::OnCheckQualityBnClicked(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	if (m_CheckBoxQuality.GetCheck())
	{
		m_SliderQuality.EnableWindow(TRUE);
	}
	else
	{
		m_SliderQuality.EnableWindow(FALSE);
	}	

	return 0;
}

LRESULT CFrogImageResizeDialog::OnCheckPutInFolderBnClicked(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	if (m_CheckBoxPutInFolder.GetCheck())
	{
		m_EditFolder.EnableWindow(TRUE);
	}
	else
	{
		m_EditFolder.EnableWindow(FALSE);
	}

	return 0;
}

void CFrogImageResizeDialog::EnableDisableControls()
{
	if (m_RadioSize.GetCheck())
	{
		m_SliderPercent.EnableWindow(FALSE);
		m_EditWidth.EnableWindow(TRUE);
		m_EditHeight.EnableWindow(TRUE);
		m_LabelWidth.EnableWindow(TRUE);
		m_LabelHeight.EnableWindow(TRUE);
	}
	else
	{
		m_SliderPercent.EnableWindow(TRUE);

		m_EditWidth.EnableWindow(FALSE);
		m_EditHeight.EnableWindow(FALSE);
		m_LabelWidth.EnableWindow(FALSE);
		m_LabelHeight.EnableWindow(FALSE);
	}
}
LRESULT CFrogImageResizeDialog::OnSliderPercentChange(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
{
	HWND labelPercent = GetDlgItem(IDC_STATIC_PERECNT);
	HWND sliderPercent = GetDlgItem(IDC_SLIDER_PERCENT);

	LRESULT lResult = ::SendMessage(sliderPercent, (UINT)TBM_GETPOS, 0, 0);

	wchar_t buffer[1024];
	_itow_s((int)lResult, buffer, 1024, 10);
	wstring label = buffer;
	label = label + L"%";

	::SetWindowText(labelPercent, label.c_str());

	return 0;
}

LRESULT CFrogImageResizeDialog::OnSliderQualityChange(int idCtrl, LPNMHDR pnmh, BOOL& bHandled)
{
	HWND labelQuality = GetDlgItem(IDC_STATIC_QUALITY);
	HWND sliderQuality = GetDlgItem(IDC_SLIDER_QUALITY);

	LRESULT lResult = ::SendMessage(sliderQuality, (UINT)TBM_GETPOS, 0, 0);

	wchar_t buffer[1024];
	_itow_s((int)lResult, buffer, 1024, 10);

	::SetWindowText(labelQuality, buffer);

	return 0;
}
